package com.hh.bss.project.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.hh.bss.auth.client.OnlineManager;
import com.hh.bss.auth.client.OnlineUser;
import com.hh.bss.base.BaseSpringController;
import com.hh.bss.common.page.Page;
import com.hh.bss.common.page.PageRequest;
import com.hh.bss.project.entity.AdvancePayment;
import com.hh.bss.project.entity.ContractJobCvCrm;
import com.hh.bss.project.service.AccountPnService;
import com.hh.bss.project.service.AdvancePaymentService;
import com.hh.bss.project.vo.query.AdvancePaymentQuery;
import com.hh.bss.util.BeanUtils;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;
@Controller
public class AdvancePaymentController extends BaseSpringController {
	// 默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;
	Integer id = null;
	@SuppressWarnings("unused")
	private String[] items;

	@Autowired
	private AdvancePaymentService advancePaymentService;
	
	@Autowired
	private AccountPnService accountPnService;

	/** 通过spring自动注入 */

	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	public void get(HttpServletRequest request, HttpServletResponse response, AdvancePayment advancePayment) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			AdvancePayment payment = (AdvancePayment) advancePaymentService.getById(advancePayment.getId());

			if (payment != null){
				result.put("data", payment);
			}
			result.put("success", true);
			result.put("msg", "成功!");

		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	/**
	 * ExtGrid使用 列表
	 * 
	 * @throws IOException
	 */
	public void list(HttpServletRequest request, HttpServletResponse response, AdvancePaymentQuery query) throws IOException {

		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		ListRange<AdvancePayment> resultList = new ListRange();

		try {
			OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				OnlineUser onlineUser = onlineManager.getOnlineUser();
				query.setOnlineUser(onlineUser);
			}
			Page page = advancePaymentService.findPage(query);

			List<AdvancePayment> advancePaymentList = page.getResult();
			resultList.setRows(advancePaymentList);
			resultList.setTotal(page.getTotalCount());
			resultList.setMessage("ok");
			resultList.setSuccess(true);
		} catch (Exception e) {
			resultList.setMessage("系统异常");
			resultList.setSuccess(false);
			e.printStackTrace();
		}

		outJson(response, resultList);

	}

	/**
	 * extGrid保存
	 * 
	 * @throws IOException
	 */
	public void save(HttpServletRequest request, HttpServletResponse response, AdvancePayment advancePayment) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			// 设置创建用户和创建时间
			OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null && BeanUtils.isExistsProperty(AdvancePayment.class, "creator")) {
					try {
						BeanUtils.setProperty(advancePayment, "creator", onlineUser.getUsername());
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			}

			if (BeanUtils.isExistsProperty(AdvancePayment.class, "createDt")) {
				try {
					BeanUtils.setProperty(advancePayment, "createDt", new Date());
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
			List list = accountPnService.findAccountPnListByNbr(advancePayment.getNbr());
			if(list.size() == 0){
				advancePaymentService.save(advancePayment);
				result.put("success", true);
				result.put("msg", "添加成功!");
			}else{
				result.put("failure", true);
				result.put("msg", "账单编号已经存在.");
			}
			
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	/**
	 * extGrid修改
	 * 
	 * @throws IOException
	 */
	public void update(HttpServletRequest request, HttpServletResponse response, AdvancePayment advancePayment) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			AdvancePayment payment = (AdvancePayment) advancePaymentService.getById(advancePayment.getId());
			bind(request, payment);
			// 设置修改用户和修改时间
			OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null && com.hh.bss.util.BeanUtils.isExistsProperty(AdvancePayment.class, "modifier")) {
					try {
						com.hh.bss.util.BeanUtils.setProperty(advancePayment, "modifier", onlineUser.getUsername());
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			}

			if (com.hh.bss.util.BeanUtils.isExistsProperty(AdvancePayment.class, "modifiedDt")) {
				try {
					com.hh.bss.util.BeanUtils.setProperty(advancePayment, "modifiedDt", new Date());
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
			this.advancePaymentService.update(payment);
			result.put("success", true);
			result.put("msg", "修改成功!");

		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getStackTrace());
			e.printStackTrace();
		}

		outJson(response, result);
	}
	
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Integer id = new java.lang.Integer((String)idarray[i]);
				advancePaymentService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
			outJson(response, result);
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
	}
		
}
